package com.infoland.afanmall.model.order.adapter;


import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.dialog.OpenIsOk_2_Dialog;
import com.infoland.afanmall.base.util.CacheKit;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.model.order.entity.EEEEE;
import com.infoland.afanmall.model.order.entity.OrderItemItemEntity;
import com.infoland.afanmall.model.order.entity.OrderItemListBean;
import com.infoland.afanmall.model.order.entity.OrderSelectResponse;
import com.infoland.afanmall.model.order.linster.OrderDeleteLinster;
import com.infoland.afanmall.model.order.linster.OrderInfoLinster;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AllOrderAdapter extends BaseQuickAdapter<OrderSelectResponse.ListBean, BaseViewHolder> {
    OpenIsOk_2_Dialog openIsOk_2_dialog = null;
    OrderDeleteLinster orderDeleteLinster;
    FragmentActivity activity;
    RecyclerView orderMyItemRecy;
    Order_Item_Item_Adapter order_item_item_adapter;

    @Override
    protected void convert(BaseViewHolder helper, OrderSelectResponse.ListBean item) {
        helper.setText(R.id.order_my_item_price, "￥" + item.getO_payment_amount() + "");
        helper.setText(R.id.order_my_item_count, "共" + item.getGoods_count() + "件");
        helper.setText(R.id.order_my_item_time, item.getCreate_time() + "");
        setStatus(helper.getView(R.id.order_my_item_status), item);
        ImageView orderMyItemImg = helper.getView(R.id.order_my_item_img);
        orderMyItemImg.setOnClickListener(v -> {
            ToolsKit.setSleep(orderMyItemImg, 1000);
            deleteOrder(item.getId(), helper.getAdapterPosition());
        });
        setItemOrderAdapter(helper, item.getOrderItemList());
    }

    /*** 设置订单显示状态**/
    private void setStatus(TextView view, OrderSelectResponse.ListBean item) {
        int o_status = item.getO_status();//订单状态 0交易关闭 1交易中 2交易完成
        int o_pay_status = item.getO_pay_status();//订单付款状态 0未付款 1已付款
        int o_send_status = item.getO_send_status();//订单发货状态 0未发货 1已发货 2已收货/订单使用状态 0未使用 1部分使用 2已使用
        String o_send_status_v = item.getO_send_status_v();//订单发货状态
        switch (o_status) {
            /*** 0交易关闭**/
            case 0:
                view.setText("交易关闭");
                break;
            /*** 1交易中**/
            case 1:
                switch (o_pay_status) {
                    /*** 0未付款**/
                    case 0:
                        view.setText("代付款");
                        break;
                    /*** 1已付款**/
                    case 1:
                        switch (o_send_status) {
                            /*** 0未发货**/
                            case 0:
                                view.setText("未发货");
                                break;
                            /*** 1已发货**/
                            case 1:
                                view.setText("已发货");
                                break;
                            /*** 2已收货**/
                            case 2:
                                view.setText("已收货");
                                break;
                        }
                        break;
                }
                break;
            /*** 2交易完成**/
            case 2:
                view.setText("交易完成");
                break;
            default:
                view.setText(o_send_status_v);
                break;
        }

    }

    /*** 构建子订单适配器**/
    private void setItemOrderAdapter(BaseViewHolder helper, List<OrderSelectResponse.ListBean.OrderItemListBean> orderItemList) {
        LogKit.E("  setItemOrderAdapter ", "  setItemOrderAdapter");
        orderMyItemRecy = helper.getView(R.id.order_my_item_recy);
        order_item_item_adapter = new Order_Item_Item_Adapter(R.layout.order_my_item_item, orderItemList);
        order_item_item_adapter.setOrderInfoLinster(item -> {
            orderInfoLinster.click(item);
        });
        orderMyItemRecy.setLayoutManager(new LinearLayoutManager(activity.getApplicationContext()));
        orderMyItemRecy.setAdapter(order_item_item_adapter);

    }

    /*** 删除订单**/
    private void deleteOrder(int id, int adapterPosition) {
        openIsOk_2_dialog = ToolsKit.openDialog_2("确认删除订单?", "",
                "取消", "确定", activity,
                v -> {
                    if (openIsOk_2_dialog != null) {
                        openIsOk_2_dialog.dismiss();
                    }
                },
                v -> {
                    orderDeleteLinster.delete(id, adapterPosition);
                    openIsOk_2_dialog.dismiss();
                });
        openIsOk_2_dialog.setCancelable(false);
    }

    public AllOrderAdapter(int layoutResId, @Nullable List<OrderSelectResponse.ListBean> data, FragmentActivity activity) {
        super(R.layout.order_my_item, data);
        this.activity = activity;
    }

    public void setOrderDeleteLinster(OrderDeleteLinster orderDeleteLinster) {
        this.orderDeleteLinster = orderDeleteLinster;
    }

    OrderInfoLinster orderInfoLinster;

    public void setOrderInfoLinster(OrderInfoLinster orderInfoLinster) {
        this.orderInfoLinster = orderInfoLinster;
    }

}